/*
 * DatabaseHandler.cpp
 *
 *  Created on: 2010.08.21.
 *      Author: andris
 */

#include "DatabaseHandler.h"
#include <FBase.h>
#include <FUi.h>
#include <FIo.h>

using namespace Osp::Base;
using namespace Osp::Io;

DatabaseHandler::DatabaseHandler() {
}

DatabaseHandler::~DatabaseHandler() {
	delete database;
	delete enumerator;
	delete statement;
	delete itemList;
}

result DatabaseHandler::Construct() {
	result r = E_SUCCESS;
	String query;

	enumerator = NULL;
	statement = NULL;

	// Create the database object (also the file, if it does not exist)
	database = new Osp::Io::Database();
	r = database->Construct("/Home/jelszotar.db", true);
	if (IsFailed(r)) {
		AppLog("Could not construct db");
	}

	// create the Images table within DB, if it did not exist
	query.Append(L"CREATE TABLE IF NOT EXISTS asd ( name TEXT PRIMARY KEY, data TEXT )");

	r = database->ExecuteSql(query,true);

	String dbname;
	dbname = "/Home/jelszotar.db";
	if (database->Exists(dbname)) AppLog("database created successfully");

	if(IsFailed(r))
	{
		AppLog("Could not create table on db");
	}

	return r;
}

result DatabaseHandler::Persist(Osp::Base::String newName,
		Osp::Base::String newPwd) {
	result r = E_SUCCESS;
	String query;
	database->BeginTransaction();
	query.Append(L"INSERT INTO asd (name, data) values (?,?)");
	statement = database->CreateStatementN(query);
	statement->BindString(0,newName);
	statement->BindString(1,newPwd);
	enumerator = database->ExecuteStatementN(*statement);
	r = database->CommitTransaction();
	if(IsFailed(r))
	AppLog("inserting item failed");
	return r;
}

result DatabaseHandler::Remove(Osp::Base::String name) {
	result r = E_SUCCESS;
	String query;
	database->BeginTransaction();
	query.Append(L"DELETE FROM asd WHERE name = ?");
	statement = database->CreateStatementN(query);
	statement->BindString(0,name);
			enumerator = database->ExecuteStatementN(*statement);
			r = database->CommitTransaction();
			if(IsFailed(r))
			AppLog("failed deleting row");
			return r;
		}

result DatabaseHandler::Update() {
	result r = E_SUCCESS;
	return r;
}

Osp::Io::DbEnumerator * DatabaseHandler::getAllItems(bool sorrend) {

	String query;
	if (sorrend)
		query.Append(L"SELECT name,data FROM asd ORDER BY name ASC");
		else
		query.Append(L"SELECT name,data FROM asd ORDER BY name DESC");
		statement = database->CreateStatementN(query);
		enumerator = database->ExecuteStatementN(*statement);

		if(!enumerator) AppLog("a lekerdezes nem adott vissza eredmenyt");
		return enumerator;
	}

Osp::Io::DbEnumerator * DatabaseHandler::getNItems(bool sorrend, int page,
		int rpp) {
	String query;
	if (sorrend)
		query.Append(L"SELECT name,data FROM asd ORDER BY data ASC LIMIT ?,?");
	else
		query.Append(L"SELECT name,data FROM asd ORDER BY data DESC LIMIT ?,?");
	statement = database->CreateStatementN(query);
	int start = page * rpp;
	statement->BindInt(0,start);
	statement->BindInt(1,rpp);
	enumerator = database->ExecuteStatementN(*statement);

	if(!enumerator) AppLog("anyad");
	return enumerator;
}
